{% extends 'bike/base.html' %}

{% block title %}统计信息 - 自行车管理系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2><i class="bi bi-bar-chart"></i> 统计信息</h2>
            <a href="{% url 'bike:bike_list' %}" class="btn btn-outline-secondary">
                <i class="bi bi-list-ul"></i> 自行车列表
            </a>
        </div>

        <!-- 总体统计 -->
        <div class="row mb-4">
            <div class="col-md-4">
                <div class="card stats-card">
                    <div class="card-body text-center">
                        <i class="bi bi-bicycle display-4 mb-3"></i>
                        <div class="stats-number">{{ total_bikes }}</div>
                        <div>总自行车数量</div>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card stats-card">
                    <div class="card-body text-center">
                        <i class="bi bi-exclamation-triangle display-4 mb-3"></i>
                        <div class="stats-number">{{ total_violations }}</div>
                        <div>违章记录数量</div>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card stats-card">
                    <div class="card-body text-center">
                        <i class="bi bi-exclamation-triangle display-4 mb-3"></i>
                        <div class="stats-number">{{ violation_stats.resolved|add:violation_stats.dismissed }}</div>
                        <div>违章已处理</div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <!-- 按类别统计 -->
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0">
                            <i class="bi bi-tags"></i> 按类别统计
                        </h5>
                    </div>
                    <div class="card-body">
                        {% if category_stats %}
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead>
                                    <tr>
                                        <th>类别</th>
                                        <th>数量</th>
                                        <th>占比</th>
                                        <th>进度条</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for category, count in category_stats.items %}
                                    {% if count > 0 %}
                                    <tr>
                                        <td>{{ category }}</td>
                                        <td>
                                            <span class="badge bg-primary">{{ count }}</span>
                                        </td>
                                        <td>
                                            {% widthratio count total_bikes 100 %}%
                                        </td>
                                        <td>
                                            <div class="progress" style="height: 20px;">
                                                <div class="progress-bar" role="progressbar" 
                                                     style="width: {% widthratio count total_bikes 100 %}%"
                                                     aria-valuenow="{% widthratio count total_bikes 100 %}" 
                                                     aria-valuemin="0" aria-valuemax="100">
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                    {% endif %}
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                        {% else %}
                        <div class="text-center py-4">
                            <i class="bi bi-graph-down text-muted display-4"></i>
                            <p class="text-muted mt-2">暂无统计数据</p>
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>

            <!-- 按学院统计 -->
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0">
                            <i class="bi bi-building"></i> 按学院统计
                        </h5>
                    </div>
                    <div class="card-body">
                        {% if college_stats %}
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead>
                                    <tr>
                                        <th>学院</th>
                                        <th>自行车数量</th>
                                        <th>占比</th>
                                        <th>违章已处理</th>
                                        <th>进度条</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for college, count in college_stats.items %}
                                    {% if count > 0 %}
                                    <tr>
                                        <td>{{ college }}</td>
                                        <td>
                                            <span class="badge bg-secondary">{{ count }}</span>
                                        </td>
                                        <td>
                                            {% widthratio count total_bikes 100 %}%
                                        </td>
                                        <td>
                                            {% for violation_stat in college_violation_stats %}
                                                {% if violation_stat.college == college %}
                                                    <span class="badge bg-success">{{ violation_stat.processed_count }}</span>
                                                {% endif %}
                                            {% empty %}
                                                <span class="badge bg-success">0</span>
                                            {% endfor %}
                                        </td>
                                        <td>
                                            <div class="progress" style="height: 20px;">
                                                <div class="progress-bar bg-secondary" role="progressbar" 
                                                     style="width: {% widthratio count total_bikes 100 %}%"
                                                     aria-valuenow="{% widthratio count total_bikes 100 %}" 
                                                     aria-valuemin="0" aria-valuemax="100">
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                    {% endif %}
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                        {% else %}
                        <div class="text-center py-4">
                            <i class="bi bi-graph-down text-muted display-4"></i>
                            <p class="text-muted mt-2">暂无统计数据</p>
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>

        <!-- 快速操作 -->
        <div class="row mt-4">
            <div class="col-12">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0">
                            <i class="bi bi-lightning"></i> 快速操作
                        </h5>
                    </div>
                    <div class="card-body">
                        <div class="d-flex gap-2 flex-wrap">
                            <a href="{% url 'bike:bike_list' %}" class="btn btn-primary">
                                <i class="bi bi-list-ul"></i> 查看所有自行车
                            </a>
                            {% if user_permissions.is_authenticated %}
                                {% if user_permissions.is_admin or user_permissions.is_super_admin %}
                                    <a href="{% url 'bike:admin_bike_add' %}" class="btn btn-success">
                                        <i class="bi bi-plus-circle"></i> 添加新自行车
                                    </a>
                                    <a href="{% url 'bike:admin_dashboard' %}" class="btn btn-info">
                                        <i class="bi bi-gear"></i> 管理后台
                                    </a>
                                    {% if user_permissions.college %}
                                        <small class="text-muted d-flex align-items-center">
                                            <i class="bi bi-info-circle me-1"></i>
                                            可添加 {{ user_permissions.college_display }} 的自行车
                                        </small>
                                    {% endif %}
                                {% else %}
                                    <a href="{% url 'bike:admin_login' %}" class="btn btn-warning">
                                        <i class="bi bi-shield-lock"></i> 需要管理员权限
                                    </a>
                                {% endif %}
                            {% else %}
                                <a href="{% url 'bike:admin_login' %}" class="btn btn-success">
                                    <i class="bi bi-plus-circle"></i> 添加新自行车
                                </a>
                                <a href="{% url 'bike:admin_login' %}" class="btn btn-info">
                                    <i class="bi bi-gear"></i> 管理后台
                                </a>
                                <small class="text-muted d-flex align-items-center">
                                    <i class="bi bi-info-circle me-1"></i>
                                    请先登录以添加自行车
                                </small>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
